﻿using System;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Data.Models.Org;
using ZHDJ.JoinParty.BLL;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;
using ZHDJ.PayFactory;
using ZHDJ.PayFactory.Model;
using ZHDJ.PayFactory.Common;
using System.Web;
using ZHDJ.Data.Models.Center;
using System.Data;
using ZHDJ.Data.BLL;

namespace ZHDJ.Web.Portal
{
    public partial class MemberCost : UseMorePageMasterPage
    {
        protected string MonthHtmlString = string.Empty;
        protected double CostBase = 0.0;
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true, true);
            LoadData();
        }

        #region 加载数据
        private void LoadData()
        {
            StringBuilder sb = new StringBuilder();
            
            DataTable dt = PartyMemberCost.GetBranchCost(0, 1, CurrentUser.BranchID, DateTime.Now.Year, CurrentUser.UserName).Tables[0];
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                CostBase = Convert.ToDouble(dr["CostBase"]);
                //党费基数
                if (CostBase > 0)
                {
                    string[] array = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一", "十二" };
                    for (int i = 0; i < 12; i++)
                    {
                        sb.Append("<div class=\"checkbox\"/>");
                        sb.Append("<label>");
                        if (Convert.ToDouble(dr["M" + (i + 1).ToString()]) > 0)
                            sb.Append("<div><input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\" /></div>");
                        else
                            sb.Append("<div><input name=\"ckCost\" type=\"checkbox\" data-month=\"" + (i + 1).ToString() + "\" value=\"" + CostBase + "\" onclick=\"return sumCost();\" /></div>");
                        sb.Append("<div> " + array[i] + " </div>");
                        sb.Append("</label>");
                        sb.Append("</div>");
                    }
                    main.Visible = true;
                    message.Visible = false;
                }
                else
                {
                    main.Visible = false;
                    message.Visible = true;
                }
            }
            else
            {
                main.Visible = false;
                message.Visible = true;
            }
            MonthHtmlString = sb.ToString();

            if (!IsPostBack)
            {
                txtpaymoney.Value = "0";
            }
        }
        #endregion

        #region 支付操作
        /// <summary>
        /// 支付操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPay_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrWhiteSpace(hidMonths.Value))
            {
                Alert("请选择要缴纳的月份！");
                return;
            }
            if (string.IsNullOrWhiteSpace(txtpaymoney.Value) || txtpaymoney.Value == "0")
            {
                Alert("请填写需要交费金额！");
                return;
            }
            if (rdoAlipay.Checked == false && rdoWxpay.Checked == false)
            {
                Alert("请选择一种支付方式");
                return;
            }
            #endregion

            string paymoney = txtpaymoney.Value;
            string[] monthsArray = hidMonths.Value.TrimEnd(',').Split(',');
            CommonModel model = new CommonModel();
            model.out_trade_no = OrderHelper.GenerateId();
            model.subject = "缴纳党费";
            model.body = "缴纳党费";

            #region 添加党费缴纳订单
            for (int i = 0; i < monthsArray.Length; i++)
            {
                PartyMemberCost current = new PartyMemberCost();
                current.ID = StringHelper.NewID;
                current.LoginID = CurrentUser.LoginID;
                current.Status = 5;//待付款
                current.CreateTime = DateTime.Now;
                current.ModifyTime = DateTime.Now;
                current.CreateBy = CurrentUser.LoginID;
                current.ModifyBy = CurrentUser.LoginID;
                current.Month = monthsArray[i];
                current.PayDate = DateTime.Now;
                current.PayWay = "2";
                current.Year = DateTime.Now.Year.ToString();
                current.Cost = Convert.ToDecimal(CostBase);
                current.OrderNumber = model.out_trade_no;
                DB.PartyMemberCosts.Add(current);
                DB.SaveChanges();
            }
            #endregion

            if (rdoAlipay.Checked == true)
            {
                #region 支付宝
                model.total_fee = paymoney;
                Payment Alipay = PaymentFactory.CreatePaymentFactory("AliPay");
                string result = Alipay.Pay(model);
                Response.Write(result);
                #endregion
            }
            else if (rdoWxpay.Checked == true)
            {
                #region 微信
                model.total_fee = (Convert.ToDouble(paymoney) * 100).ToString();
                Payment WxPay = PaymentFactory.CreatePaymentFactory("WxPay");
                string result = WxPay.Pay(model);
                Response.Write("<script language='javascript'>window.location='WxQRCode.aspx?data=" + result + "'</script>");
                #endregion
            }
        }
        #endregion
    }
}